#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMouseEvent>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);
    m_pFile = new FileWindow();
    m_pFriend = new FriendWindow();
    m_pMine = new MineWindow();
    ui->stackedWidget->addWidget(m_pFile);
    ui->stackedWidget->addWidget(m_pFriend);
    ui->stackedWidget->addWidget(m_pMine);

    btnGroup.addButton(ui->btnfile, 0);
    btnGroup.addButton(ui->btnfriend, 1);
    btnGroup.addButton(ui->btnmine, 2);

    connect(&btnGroup, static_cast<void (QButtonGroup::*)(int)>(&QButtonGroup::buttonClicked),
                ui->stackedWidget, &QStackedWidget::setCurrentIndex);

    btnGroup.button(0)->setChecked(true);
    ui->stackedWidget->setCurrentIndex(0);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    auto rect = ui->title->geometry();
    auto p= event->pos();
    if(rect.contains(p))
    {
         m_isPressed = true;
         m_wnd = this ->geometry().topLeft();
         press = this ->mapToGlobal(p);
    }
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    m_isPressed = false;
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if(m_isPressed)
    {
       auto v = mapToGlobal(event->pos())-press;
       auto r = m_wnd;
       this ->move(r+v);
    }
}

MainWindow &MainWindow::getInstance()
{
    static MainWindow instance;
    return instance;
}

FriendWindow *MainWindow::getPFriend() const
{
    return m_pFriend;
}

void MainWindow::setUserLabel(const char *name)
{
    ui->label->setText(name);
}

FileWindow *MainWindow::getFileWindow() const
{
    return m_pFile;
}



void MainWindow::on_btnmin_clicked()
{
    showMinimized();
}

void MainWindow::on_btnmax_clicked()
{
    if (isMaximized) {
            showNormal();
            isMaximized = false;
        } else {
            showMaximized();
            isMaximized = true;
        }
}

void MainWindow::on_btnclose_clicked()
{
    exit(0);
}
